<?php
/* SVN FILE: $Id: assessment_ratings_controller.php 34 2009-10-17 13:42:12Z kenreily@gmail.com $ */
/**
 * Short description for file.
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * Recenseo: Online submission and reviewing for education
 * Copyright 2008-2009, Ken Reily
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * $Date: 2009-10-17 13:42:12 +0000 (Sat, 17 Oct 2009) $
 * $Revision: 34 $
 * $Author: kenreily@gmail.com $
 * $HeadURL: http://recenseo.googlecode.com/svn/trunk/app/controllers/assessment_ratings_controller.php $
 */
class AssessmentRatingsController extends AppController
{
  var $name = 'AssessmentRatings';
  var $helpers = array('Time');
  var $uses = array('AssessmentRating','Assessment','Submission','AssessmentTestCase','AssessmentQualityAttribute','Assignment');
  
  function edit($id)
  {
    $this->AssessmentRating->id = $id;
    $asmt_rating = $this->AssessmentRating->read();
    $submission = $this->AssessmentRating->Assessment->Submission->findById($asmt_rating['Assessment']['submission_id']);

    // Don't let anyone other than the submission author post edits to this review.
    if ($submission['Submission']['user_id'] != $this->Session->read('Auth.User.id'))
    {
      $this->flash('Access denied.','/');
    }
    else if (!empty($asmt_rating['Assessment']['ratings_submitted']))
    {
      $this->flash('Ratings have already been submitted.',"/assessments/view/{$assessment_id}");
    }    
    
    if (!empty($this->data))
    {
      if ($this->AssessmentRating->save($this->data))
      {
        $assessment_id = $this->data['AssessmentRating']['assessment_id'];
        $this->flash('Your response has been saved.',"/assessments/view/{$assessment_id}");
      }
    }
    else
    {
      $this->data = $asmt_rating;
    }
    
    $this->set('asmt_rating',$asmt_rating);
    $this->set('likert_results',$this->AssessmentRating->LikertResult->find('list'));   
  }

  // Add selects a random assessment and creates a rating for it.
  function add($assignment_id = null)
  {
    if (!empty($this->data))
    {
      if ($this->AssessmentRating->save($this->data))
      {
	// Clear the data for the new assessment.
	$this->data['AssessmentRating']['rating'] = null;
	$this->data['AssessmentRating']['comments'] = null;

	$params = 
	  array('conditions' => array('Submission.assignment_id' => $assignment_id),
		'fields' => 'Assessment.id');
	$assessments = $this->Assessment->find('all',$params);

	$params = 
	  array('conditions' => array('Assignment.id' => $assignment_id),
		'fields' => array('Assignment.id',
				  'Assignment.course_id',
				  'Assignment.required_ratings'),
		'recursive' => -1);
	$assignment = $this->Assignment->find('first',$params);
	$required_ratings = $assignment['Assignment']['required_ratings'];

	$rating_count = 1;

	foreach ($assessments as $assessment)
	{
	  foreach ($assessment['AssessmentRating'] as $rating)
	  {
	    if ($rating['user_id'] == $this->Session->read('Auth.User.id'))
	    {
	      $rating_count++;
	    }
	  }
	}

	if ($rating_count > $required_ratings)
	{
	  $this->redirect("/assignments/index/{$assignment['Assignment']['course_id']}");
	}
	else
	{  
	  $this->Session->setFlash("Save successful! Now rating assessment {$rating_count} of {$required_ratings}.");
	}

      }
      else
      {
	$assessment_id = $this->data['AssessmentRating']['assessment_id'];
      }
    }

    // First, make sure this user has completed their assigned assessments.
    $asmt_count = 
      $this->Assessment->countForUserAssignment($this->Session->read('Auth.User.id'),
						$assignment_id);
    if ($asmt_count == 0)
    {
      if (isset($assessment_id))
      {
	$params = 
	  array('conditions' => array('Assessment.id' => $assessment_id),
		'fields' => array('Assessment.id',
				  'Submission.id'),
		'recursive' => 0);

	$assessment = 
	  $this->Assessment->find('first',$params);
      }
      else
      {
	$assessment = 
	  $this->Assessment->getRandomForUserAssignment($this->Session->read('Auth.User.id'),
							$assignment_id);
      }

      if (!empty($assessment))
      {
	$this->data['AssessmentRating']['assessment_id'] = $assessment['Assessment']['id'];
	$this->data['AssessmentRating']['user_id'] = $this->Session->read('Auth.User.id');
	
	$params = 
	  array('conditions' => array('Submission.id' => $assessment['Submission']['id']),
		'fields' => array('Assignment.name',
				  'Assignment.id',
				  'GradingOption.name',
				  'Submission.comments'),
		'recursive' => 0);
	$submission = $this->Submission->find('first',$params);
	$this->set('submission',$submission);
	
	$params = 
	  array('conditions' => array('Assessment.id' => $assessment['Assessment']['id']));
	$test_cases = $this->AssessmentTestCase->find('all',$params);
	$this->set('test_cases',$test_cases);
	
	$qualities = $this->AssessmentQualityAttribute->find('all',$params);
	$this->set('qualities',$qualities);
      }
    }
    else
    {
      $this->Session->setFlash('No assessments available for rating.');
    }      
  }
}
?>